package com.rightsidetech.http.controller.admin.news;

import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.news.NewsTypeSaveReq;
import com.rightsidetech.system.dtos.request.news.NewsTypeUpdateReq;
import com.rightsidetech.system.dtos.response.news.NewsTypePageResp;
import com.rightsidetech.system.service.SysNewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/news_type/")
@ShenyuSpringMvcClient(path = "/admin/news_type/**")
@Api(tags = "新闻分类相关")
public class SysNewsTypeController extends BaseController {

    @Resource
    private SysNewsTypeService sysNewsTypeService;


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/save")
    @ApiOperation("添加新闻分类")
    public ResponseData saveType(@RequestBody NewsTypeSaveReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return sysNewsTypeService.save(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("编辑新闻分类")
    public ResponseData update(@RequestBody NewsTypeUpdateReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return sysNewsTypeService.update(req,loginName,loginInfoId);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("新闻分类分页列表")
    public ResponseData pageList(PageRequest req) throws Exception {
        return sysNewsTypeService.page(req);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/search")
    @ApiOperation("新闻分类下拉选")
    public ResponseData<List<NewsTypePageResp>> selectList(){
         return sysNewsTypeService.selectList();
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除新闻分类")
    public ResponseData delType(@RequestBody DelReq req){
        return sysNewsTypeService.del(req);
    }
}
